from typing import Optional
from datetime import datetime, date
from sqlmodel import SQLModel, Field
from .resBase import BaseResponse, PaginationRequest, PaginatedResponse, PaginationInfo


# 欧洲赔率预测表模型
class MatchPredictEurope168(SQLModel, table=True):
    __tablename__: str = "t_match_predict_europe_168"
    
    fid: Optional[int] = Field(default=None, primary_key=True)
    season_id: Optional[int] = Field(default=None)
    homeid: Optional[int] = Field(default=None)
    awayid: Optional[int] = Field(default=None)
    event_name: Optional[str] = Field(default=None, max_length=100)
    order_num: Optional[str] = Field(default=None, max_length=20)
    ownerdate: Optional[date] = Field(default=None)
    matchtime: Optional[datetime] = Field(default=None)
    homesxname: Optional[str] = Field(default=None, max_length=100)
    awaysxname: Optional[str] = Field(default=None, max_length=100)
    home_initial: Optional[float] = Field(default=None)
    draw_initial: Optional[float] = Field(default=None)
    away_initial: Optional[float] = Field(default=None)
    home_final: Optional[float] = Field(default=None)
    draw_final: Optional[float] = Field(default=None)
    away_final: Optional[float] = Field(default=None)
    homescore: Optional[int] = Field(default=None)
    awayscore: Optional[int] = Field(default=None)
    prob_home_win: Optional[float] = Field(default=None)
    prob_draw: Optional[float] = Field(default=None)
    prob_away_win: Optional[float] = Field(default=None)


# 查询条件模型
class MatchPredictEurope168Condition(PaginationRequest):
    event_name: Optional[str] = Field(default=None, description="赛事名称")
    order_num: Optional[str] = Field(default=None, description="订单号")
    homesxname: Optional[str] = Field(default=None, description="主队名称")
    awaysxname: Optional[str] = Field(default=None, description="客队名称")
    s_time: Optional[str] = Field(default=None, description="比赛开始时间 yyyy-mm-dd")
    e_time: Optional[str] = Field(default=None, description="比赛结束时间 yyyy-mm-dd")
    ownerdate_s: Optional[str] = Field(default=None, description="所属日期开始 yyyy-mm-dd")
    ownerdate_e: Optional[str] = Field(default=None, description="所属日期结束 yyyy-mm-dd")
    
    # 初始赔率范围
    home_initial_min: Optional[float] = Field(default=None, description="主队初始赔率最小值")
    home_initial_max: Optional[float] = Field(default=None, description="主队初始赔率最大值")
    draw_initial_min: Optional[float] = Field(default=None, description="平局初始赔率最小值")
    draw_initial_max: Optional[float] = Field(default=None, description="平局初始赔率最大值")
    away_initial_min: Optional[float] = Field(default=None, description="客队初始赔率最小值")
    away_initial_max: Optional[float] = Field(default=None, description="客队初始赔率最大值")
    
    # 最终赔率范围
    home_final_min: Optional[float] = Field(default=None, description="主队最终赔率最小值")
    home_final_max: Optional[float] = Field(default=None, description="主队最终赔率最大值")
    draw_final_min: Optional[float] = Field(default=None, description="平局最终赔率最小值")
    draw_final_max: Optional[float] = Field(default=None, description="平局最终赔率最大值")
    away_final_min: Optional[float] = Field(default=None, description="客队最终赔率最小值")
    away_final_max: Optional[float] = Field(default=None, description="客队最终赔率最大值")
    
    # 比分范围
    homescore_min: Optional[int] = Field(default=None, description="主队得分最小值")
    homescore_max: Optional[int] = Field(default=None, description="主队得分最大值")
    awayscore_min: Optional[int] = Field(default=None, description="客队得分最小值")
    awayscore_max: Optional[int] = Field(default=None, description="客队得分最大值")
    
    # 胜负概率范围
    prob_home_win_min: Optional[float] = Field(default=None, description="主胜概率最小值")
    prob_home_win_max: Optional[float] = Field(default=None, description="主胜概率最大值")
    prob_draw_min: Optional[float] = Field(default=None, description="平局概率最小值")
    prob_draw_max: Optional[float] = Field(default=None, description="平局概率最大值")
    prob_away_win_min: Optional[float] = Field(default=None, description="客胜概率最小值")
    prob_away_win_max: Optional[float] = Field(default=None, description="客胜概率最大值")


# 历史同概查询条件
class HistoricalSimilarCondition(SQLModel):
    prob_home_win_min: Optional[float] = Field(default=None, description="主胜概率最小值")
    prob_home_win_max: Optional[float] = Field(default=None, description="主胜概率最大值")
    prob_draw_min: Optional[float] = Field(default=None, description="平局概率最小值")
    prob_draw_max: Optional[float] = Field(default=None, description="平局概率最大值")
    prob_away_win_min: Optional[float] = Field(default=None, description="客胜概率最小值")
    prob_away_win_max: Optional[float] = Field(default=None, description="客胜概率最大值")


# 历史同概统计结果
class HistoricalSimilarStats(SQLModel):
    total_matches: int = Field(description="总比赛场次")
    home_wins: int = Field(description="主胜场次")
    draws: int = Field(description="平局场次")
    away_wins: int = Field(description="客胜场次")
    home_win_rate: float = Field(description="主胜率")
    draw_rate: float = Field(description="平局率")
    away_win_rate: float = Field(description="客胜率")


# 历史同概响应模型
class HistoricalSimilarResponse(BaseResponse):
    data: Optional[HistoricalSimilarStats] = None


# 响应模型
class MatchPredictEurope168ResultResponse(PaginatedResponse):
    data: Optional[list[MatchPredictEurope168]] = None


# 爆冷分析查询条件
class ColdPlayAnalysisCondition(SQLModel):
    teamid: Optional[int] = Field(default=None, description="队伍ID")
    match_date: Optional[str] = Field(default=None, description="比赛日期 yyyy-mm-dd")
    season_id: Optional[int] = Field(default=None, description="赛季ID")


# 爆冷分析统计结果
class ColdPlayAnalysisStats(SQLModel):
    total_matches: int = Field(description="总比赛场次")
    error_matches: int = Field(description="爆冷场次")
    error_matches_rate: float = Field(description="爆冷率")


# 爆冷分析响应模型
class ColdPlayAnalysisResponse(BaseResponse):
    data: Optional[ColdPlayAnalysisStats] = None


# 历史同赔查询条件模型 (基于 t_match_europe_168 表)
class HistoricalSameOddsCondition(PaginationRequest):
    event_name: Optional[str] = Field(default=None, description="赛事名称")
    season_id: Optional[int] = Field(default=None, description="赛季ID")
    homeid: Optional[int] = Field(default=None, description="主队ID")
    awayid: Optional[int] = Field(default=None, description="客队ID")
    order_num: Optional[str] = Field(default=None, description="赛事编号")
    homesxname: Optional[str] = Field(default=None, description="主队名称")
    awaysxname: Optional[str] = Field(default=None, description="客队名称")
    
    # 日期范围字段
    ownerdate_s: Optional[str] = Field(default=None, description="所属日期开始 yyyy-mm-dd")
    ownerdate_e: Optional[str] = Field(default=None, description="所属日期结束 yyyy-mm-dd")
    matchtime_s: Optional[str] = Field(default=None, description="比赛时间开始 yyyy-mm-dd")
    matchtime_e: Optional[str] = Field(default=None, description="比赛时间结束 yyyy-mm-dd")
    
    # 比分范围字段
    homescore_min: Optional[int] = Field(default=None, description="主队得分最小值")
    homescore_max: Optional[int] = Field(default=None, description="主队得分最大值")
    awayscore_min: Optional[int] = Field(default=None, description="客队得分最小值")
    awayscore_max: Optional[int] = Field(default=None, description="客队得分最大值")
    
    # 平均积分范围字段
    home_avg_points_min: Optional[float] = Field(default=None, description="主队平均积分最小值")
    home_avg_points_max: Optional[float] = Field(default=None, description="主队平均积分最大值")
    away_avg_points_min: Optional[float] = Field(default=None, description="客队平均积分最小值")
    away_avg_points_max: Optional[float] = Field(default=None, description="客队平均积分最大值")
    
    signal_home_initial_min: Optional[float] = Field(default=None, description="主队平均积分最小值")
    signal_home_initial_max: Optional[float] = Field(default=None, description="主队平均积分最大值")
    signal_draw_initial_min: Optional[float] = Field(default=None, description="客队平均积分最小值")
    signal_draw_initial_max: Optional[float] = Field(default=None, description="客队平均积分最大值")
    
    signal_away_initial_min: Optional[float] = Field(default=None, description="客队平均积分最小值")
    signal_away_initial_max: Optional[float] = Field(default=None, description="客队平均积分最大值")
    
    
    # 各时间点的主队赔率范围
    home_odds_0_min: Optional[float] = Field(default=None, description="主队初始赔率最小值")
    home_odds_0_max: Optional[float] = Field(default=None, description="主队初始赔率最大值")
    home_odds_24_min: Optional[float] = Field(default=None, description="主队24小时赔率最小值")
    home_odds_24_max: Optional[float] = Field(default=None, description="主队24小时赔率最大值")
    home_odds_48_min: Optional[float] = Field(default=None, description="主队48小时赔率最小值")
    home_odds_48_max: Optional[float] = Field(default=None, description="主队48小时赔率最大值")
    home_odds_72_min: Optional[float] = Field(default=None, description="主队72小时赔率最小值")
    home_odds_72_max: Optional[float] = Field(default=None, description="主队72小时赔率最大值")
    home_odds_167_min: Optional[float] = Field(default=None, description="主队167小时赔率最小值")
    home_odds_167_max: Optional[float] = Field(default=None, description="主队167小时赔率最大值")
    
    # 各时间点的平局赔率范围
    draw_odds_0_min: Optional[float] = Field(default=None, description="平局初始赔率最小值")
    draw_odds_0_max: Optional[float] = Field(default=None, description="平局初始赔率最大值")
    draw_odds_24_min: Optional[float] = Field(default=None, description="平局24小时赔率最小值")
    draw_odds_24_max: Optional[float] = Field(default=None, description="平局24小时赔率最大值")
    draw_odds_48_min: Optional[float] = Field(default=None, description="平局48小时赔率最小值")
    draw_odds_48_max: Optional[float] = Field(default=None, description="平局48小时赔率最大值")
    draw_odds_72_min: Optional[float] = Field(default=None, description="平局72小时赔率最小值")
    draw_odds_72_max: Optional[float] = Field(default=None, description="平局72小时赔率最大值")
    draw_odds_167_min: Optional[float] = Field(default=None, description="平局167小时赔率最小值")
    draw_odds_167_max: Optional[float] = Field(default=None, description="平局167小时赔率最大值")
    
    # 各时间点的客队赔率范围
    away_odds_0_min: Optional[float] = Field(default=None, description="客队初始赔率最小值")
    away_odds_0_max: Optional[float] = Field(default=None, description="客队初始赔率最大值")
    away_odds_24_min: Optional[float] = Field(default=None, description="客队24小时赔率最小值")
    away_odds_24_max: Optional[float] = Field(default=None, description="客队24小时赔率最大值")
    away_odds_48_min: Optional[float] = Field(default=None, description="客队48小时赔率最小值")
    away_odds_48_max: Optional[float] = Field(default=None, description="客队48小时赔率最大值")
    away_odds_72_min: Optional[float] = Field(default=None, description="客队72小时赔率最小值")
    away_odds_72_max: Optional[float] = Field(default=None, description="客队72小时赔率最大值")
    away_odds_167_min: Optional[float] = Field(default=None, description="客队167小时赔率最小值")
    away_odds_167_max: Optional[float] = Field(default=None, description="客队167小时赔率最大值")


# 历史同赔数据模型 (对应 t_match_europe_168 表字段)
class HistoricalSameOddsData(SQLModel, table=True):
    __tablename__: str = "t_match_europe_168"
    event_name: Optional[str] = Field(default=None, description="赛事名称")
    fid: Optional[int] = Field(default=None, primary_key=True,description="比赛ID")
    season_id: Optional[int] = Field(default=None, description="赛季ID")
    homeid: Optional[int] = Field(default=None, description="主队ID")
    awayid: Optional[int] = Field(default=None, description="客队ID")
    order_num: Optional[str] = Field(default=None, description="赛事编号")
    ownerdate: Optional[str] = Field(default=None, description="所属日期")
    matchtime: Optional[str] = Field(default=None, description="比赛时间")
    homesxname: Optional[str] = Field(default=None, description="主队名称")
    awaysxname: Optional[str] = Field(default=None, description="客队名称")
    homescore: Optional[int] = Field(default=None, description="主队得分")
    awayscore: Optional[int] = Field(default=None, description="客队得分")
    home_avg_points: Optional[float] = Field(default=None, description="主队平均积分")
    away_avg_points: Optional[float] = Field(default=None, description="客队平均积分")
    signal_home_initial: Optional[float] = Field(default=None, description="主队信号值")
    signal_draw_initial: Optional[float] = Field(default=None, description="平信号值")
    signal_away_initial: Optional[float] = Field(default=None, description="客队信号值")
    home_odds_0: Optional[float] = Field(default=None, description="主队初始赔率")
    home_odds_24: Optional[float] = Field(default=None, description="主队24小时赔率")
    home_odds_48: Optional[float] = Field(default=None, description="主队48小时赔率")
    home_odds_72: Optional[float] = Field(default=None, description="主队72小时赔率")
    home_odds_167: Optional[float] = Field(default=None, description="主队167小时赔率")
    draw_odds_0: Optional[float] = Field(default=None, description="平局初始赔率")
    draw_odds_24: Optional[float] = Field(default=None, description="平局24小时赔率")
    draw_odds_48: Optional[float] = Field(default=None, description="平局48小时赔率")
    draw_odds_72: Optional[float] = Field(default=None, description="平局72小时赔率")
    draw_odds_167: Optional[float] = Field(default=None, description="平局167小时赔率")
    away_odds_0: Optional[float] = Field(default=None, description="客队初始赔率")
    away_odds_24: Optional[float] = Field(default=None, description="客队24小时赔率")
    away_odds_48: Optional[float] = Field(default=None, description="客队48小时赔率")
    away_odds_72: Optional[float] = Field(default=None, description="客队72小时赔率")
    away_odds_167: Optional[float] = Field(default=None, description="客队167小时赔率")


# 历史同赔响应模型
class HistoricalSameOddsResponse(PaginatedResponse):
    data: Optional[list[HistoricalSameOddsData]] = None


# 同赔168分析查询条件模型
class SameOdds168AnalysisCondition(SQLModel):
    event_name: Optional[str] = Field(default=None, description="赛事名称")
    matchtime_max: Optional[str] = Field(default=None, description="比赛时间最大值 yyyy-mm-dd")
    
    # 主队积分字段 
    home_avg_points_min: Optional[float] = Field(default=None, description="主队积分最小值")
    home_avg_points_max: Optional[float] = Field(default=None, description="主队积分最大值")
    
    away_avg_points_min: Optional[float] = Field(default=None, description="客队积分最小值")
    away_avg_points_max: Optional[float] = Field(default=None, description="客队积分最大值")
    # 信号值
    signal_home_initial_min: Optional[float] = Field(default=None, description="主信号值最小值")
    signal_home_initial_max: Optional[float] = Field(default=None, description="主信号值最大值")
    signal_draw_initial_min: Optional[float] = Field(default=None, description="平信号值最小值")
    signal_draw_initial_max: Optional[float] = Field(default=None, description="平信号值最大值")
    signal_away_initial_min: Optional[float] = Field(default=None, description="客信号值最小值")
    signal_away_initial_max: Optional[float] = Field(default=None, description="客信号值最大值")
    
    
    
    # 主队赔率范围字段 (所有时间点)
    home_odds_0_min: Optional[float] = Field(default=None, description="主队赔率最小值")
    home_odds_0_max: Optional[float] = Field(default=None, description="主队赔率最大值")
    
    home_odds_24_min: Optional[float] = Field(default=None, description="主队赔率最小值")
    home_odds_24_max: Optional[float] = Field(default=None, description="主队赔率最大值")
    
    home_odds_48_min: Optional[float] = Field(default=None, description="主队赔率最小值")
    home_odds_48_max: Optional[float] = Field(default=None, description="主队赔率最大值")
    
    home_odds_72_min: Optional[float] = Field(default=None, description="主队赔率最小值")
    home_odds_72_max: Optional[float] = Field(default=None, description="主队赔率最大值")
    
    home_odds_167_min: Optional[float] = Field(default=None, description="主队赔率最小值")
    home_odds_167_max: Optional[float] = Field(default=None, description="主队赔率最大值")
    
    # 平局赔率范围字段 (所有时间点)
    draw_odds_0_min: Optional[float] = Field(default=None, description="主队赔率最小值")
    draw_odds_0_max: Optional[float] = Field(default=None, description="主队赔率最大值")
    
    draw_odds_24_min: Optional[float] = Field(default=None, description="主队赔率最小值")
    draw_odds_24_max: Optional[float] = Field(default=None, description="主队赔率最大值")
    
    draw_odds_48_min: Optional[float] = Field(default=None, description="主队赔率最小值")
    draw_odds_48_max: Optional[float] = Field(default=None, description="主队赔率最大值")
    
    draw_odds_72_min: Optional[float] = Field(default=None, description="主队赔率最小值")
    draw_odds_72_max: Optional[float] = Field(default=None, description="主队赔率最大值")
    
    draw_odds_167_min: Optional[float] = Field(default=None, description="主队赔率最小值")
    draw_odds_167_max: Optional[float] = Field(default=None, description="主队赔率最大值")
    
    # 客队赔率范围字段 (所有时间点)
    away_odds_0_min: Optional[float] = Field(default=None, description="主队赔率最小值")
    away_odds_0_max: Optional[float] = Field(default=None, description="主队赔率最大值")
    
    away_odds_24_min: Optional[float] = Field(default=None, description="主队赔率最小值")
    away_odds_24_max: Optional[float] = Field(default=None, description="主队赔率最大值")
    
    away_odds_48_min: Optional[float] = Field(default=None, description="主队赔率最小值")
    away_odds_48_max: Optional[float] = Field(default=None, description="主队赔率最大值")
    
    away_odds_72_min: Optional[float] = Field(default=None, description="主队赔率最小值")
    away_odds_72_max: Optional[float] = Field(default=None, description="主队赔率最大值")
    
    away_odds_167_min: Optional[float] = Field(default=None, description="主队赔率最小值")
    away_odds_167_max: Optional[float] = Field(default=None, description="主队赔率最大值")


# 同赔168分析统计结果模型
class SameOdds168AnalysisStats(SQLModel):
    total_matches: int = Field(description="总比赛场次")
    home_wins: int = Field(description="主胜场次")
    draws: int = Field(description="平局场次")
    away_wins: int = Field(description="客胜场次")
    home_win_rate: float = Field(description="主胜率")
    draw_rate: float = Field(description="平局率")
    away_win_rate: float = Field(description="客胜率")
    most_common_score: Optional[str] = Field(default=None, description="最常见比分")
    second_most_common_score: Optional[str] = Field(default=None, description="第二常见比分")
    third_most_common_score: Optional[str] = Field(default=None, description="第三常见比分")


# 同赔168分析响应模型
class SameOdds168AnalysisResponse(BaseResponse):
    data: Optional[SameOdds168AnalysisStats] = None
